package libs.yzx.zaza.acts;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Converter;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;

/**
 * Created by Administrator on 2016/8/19
 */
public class RetrofitActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        HttpService service = stringRetrofitBuilder.create(HttpService.class);
        call2(service);
    }


    private void call1(HttpService service) {
        Call<String> call = service.getMessage("我是没用的测试参数");
        call.enqueue(new Callback<String>() {
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    String bodyString = response.body();
                    Toast.makeText(RetrofitActivity.this, bodyString, Toast.LENGTH_SHORT).show();
                } else {
                    onFailure(call, null);
                }
            }

            public void onFailure(Call<String> call, Throwable t) {
                String msg = (t == null) ? "" : t.getLocalizedMessage();
                Toast.makeText(RetrofitActivity.this, "error" + t, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void call2(HttpService client) {
        HashMap<String, String> map = new HashMap<>();
        map.put("p", "fuck you mother ~~");
        Call<String> call = client.getMessage2("test", map);
        call.enqueue(new Callback<String>() {
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    String bodyString = response.body();
                    Toast.makeText(RetrofitActivity.this, bodyString, Toast.LENGTH_SHORT).show();
                } else {
                    onFailure(call, null);
                }
            }

            public void onFailure(Call<String> call, Throwable t) {
                String msg = (t == null) ? "" : t.getLocalizedMessage();
                Toast.makeText(RetrofitActivity.this, "error" + t, Toast.LENGTH_SHORT).show();
            }
        });
    }


    /**/
    /**/
    /**/


    public interface HttpService {
        @GET("test")
        Call<String> getMessage(@Query("testParam") String testParam);
        @GET("{test}")
        Call<String> getMessage2(@Path("test") String path, @QueryMap Map<String, String> params);
    }


    public static Converter<ResponseBody, String> stringRetrofitConverter = new Converter<ResponseBody, String>() {
        public String convert(ResponseBody value) throws IOException {
            return value.string();
        }
    };

    public static Converter<ResponseBody, InputStream> inputStreamRetrofitConverter = new Converter<ResponseBody, InputStream>() {
        public InputStream convert(ResponseBody value) throws IOException {
            return value.byteStream();
        }
    };

    public static OkHttpClient client;

    static {
        client = new OkHttpClient();
    }


    public static final Retrofit stringRetrofitBuilder = new Retrofit.Builder()
            .baseUrl("http://192.168.10.117:8080/")
            .addConverterFactory(new Converter.Factory() {
                public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
                    return stringRetrofitConverter;
                }
            })
            .client(client)
            .build();

}
